﻿/** 
 * Copyright 2010 NUMKIT.COM (contact: opensource@numkit.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 * 
 * @author 陈超
 * @version 1.0
 */
class Config {
	public var aXLabel:Array;
	public var xSlide:Number = 0;
	public var xFrom:Number;
	public var xTo:Number;
	public var aYLabel:Array;
	public var ySlide:Number = 0;
	public var yFrom:Number;
	public var yTo:Number;
	public var cellWidth:Number;
	public var cellHeight:Number;
	public var aMarkLineColor:Array;
	public var aMarkLineValue:Array;
	public var leftSpace:Number;
	//color
	public var backgroundColor:Number = 0x000000;
	public var textColor:Number = 0xFFFF00;
	public var cellColor:Number = 0x008040;
	public var yAxisColor:Number = 0xFFFF00;
	public var xAxisColor:Number = 0xFFFF00;
	public var xAxisYPos:Number = 0;
	public static var formatter:TextFormat;
	public function Config(node:XMLNode) {
		if (formatter == undefined) {
			formatter = new TextFormat();
			formatter.size = 12;
		}
		if (node.nodeName != "config") {
			throw new Error("Not A config Node!");
		}
		aMarkLineColor = new Array();
		aMarkLineValue = new Array();
		var nodeName:String;
		for (var child:XMLNode = node.firstChild; child != null; child=child.nextSibling) {
			nodeName = child.nodeName;
			if (nodeName == "backgroundColor") {
				var temp = Number(child.firstChild.nodeValue);
				if (!isNaN(temp)) {
					backgroundColor = temp;
				}
			} else if (nodeName == "textColor") {
				var temp = Number(child.firstChild.nodeValue);
				if (!isNaN(temp)) {
					textColor = temp;
				}
			} else if (nodeName == "cellColor") {
				var temp = Number(child.firstChild.nodeValue);
				if (!isNaN(temp)) {
					cellColor = temp;
				}
			} else if (nodeName == "xAxisColor") {
				var temp = Number(child.firstChild.nodeValue);
				if (!isNaN(temp)) {
					xAxisColor = temp;
				}
			} else if (nodeName == "yAxisColor") {
				var temp = Number(child.firstChild.nodeValue);
				if (!isNaN(temp)) {
					yAxisColor = temp;
				}
			} else if (nodeName == "xAxis") {
				for (var grandson:XMLNode = child.firstChild; grandson != null; grandson=grandson.nextSibling) {
					nodeName = grandson.nodeName;
					if (nodeName == "label") {
						aXLabel = grandson.firstChild.nodeValue.split(",");
						var strXSlide:String = grandson.attributes["slide"];
						if (strXSlide != undefined && strXSlide != "") {
							var temp:Number = Number(strXSlide.split("%")[0]);
							if (!isNaN(temp)) {
								xSlide = temp;
							}
						}
					} else if (nodeName == "from") {
						xFrom = Number(grandson.firstChild.nodeValue);
					} else if (nodeName == "to") {
						xTo = Number(grandson.firstChild.nodeValue);
					}
				}
			} else if (nodeName == "yAxis") {
				for (var grandson:XMLNode = child.firstChild; grandson != null; grandson=grandson.nextSibling) {
					nodeName = grandson.nodeName;
					if (nodeName == "label") {
						aYLabel = grandson.firstChild.nodeValue.split(",");
						var strYSlide:String = grandson.attributes["slide"];
						if (strYSlide != undefined && strYSlide != "") {
							var temp:Number = Number(strYSlide.split("%")[0]);
							if (!isNaN(temp)) {
								ySlide = temp;
							}
						}
					} else if (nodeName == "from") {
						yFrom = Number(grandson.firstChild.nodeValue);
					} else if (nodeName == "to") {
						yTo = Number(grandson.firstChild.nodeValue);
					}
				}
			} else if (nodeName == "xAxisYPos") {
				var temp = Number(child.firstChild.nodeValue);
				if (!isNaN(temp)) {
					xAxisYPos = temp;
				}
			} else if (nodeName == "cell") {
				for (var grandson:XMLNode = child.firstChild; grandson != null; grandson=grandson.nextSibling) {
					nodeName = grandson.nodeName;
					if (nodeName == "width") {
						cellWidth = Number(grandson.firstChild.nodeValue);
					} else if (nodeName == "height") {
						cellHeight = Number(grandson.firstChild.nodeValue);
					}
				}
			} else if (nodeName == "markLines") {
				for (var grandson:XMLNode = child.firstChild; grandson != null; grandson=grandson.nextSibling) {
					nodeName = grandson.nodeName;
					if (nodeName == "markLine") {
						var value = Number(grandson.firstChild.nodeValue);
						if (value != undefined && !isNaN(value)) {
							aMarkLineValue.push(value);
							var color = Number(grandson.attributes["color"]);
							if (color == undefined || isNaN(color)) {
								color = 0xFFFF00;
							}
							aMarkLineColor.push(color);
						}
					}
				}
			}
		}
		if (aXLabel == undefined) {
			throw new Error("Can't find xAxis.label node!");
		}
		if (xFrom == undefined || isNaN(xFrom)) {
			throw new Error("Can't find xAxis.from node or invalid!");
		}
		if (xTo == undefined || isNaN(xTo)) {
			throw new Error("Can't find xAxis.to node or invalid!");
		}
		if (aYLabel == undefined) {
			throw new Error("Can't find yAxis.label node!");
		}
		if (yFrom == undefined || isNaN(yFrom)) {
			throw new Error("Can't find yAxis.from node or invalid!");
		}
		if (yTo == undefined || isNaN(yTo)) {
			throw new Error("Can't find yAxis.to node or invalid!");
		}
		if (cellWidth == undefined || isNaN(cellWidth)) {
			throw new Error("Can't find cell.width node or invalid!");
		}
		if (cellHeight == undefined || isNaN(cellHeight)) {
			throw new Error("Can't find cell.height node or invalid!");
		}
		for (var i:Number = 0; i<aXLabel.length; i++) {
			if (aXLabel[i] == "[Empty]") {
				aXLabel[i] = "";
			}
		}
		for (var i:Number = 0; i<aYLabel.length; i++) {
			if (aYLabel[i] == "[Empty]") {
				aYLabel[i] = "";
			}
		}
		//计算Y轴标签字体空间宽度
		var maxSpace:Number = 0;
		for (var i:Number = 0; i<aYLabel.length; i++) {
			var fontSize:Object = formatter.getTextExtent(aYLabel[i]);
			if (fontSize.textFieldWidth>maxSpace) {
				maxSpace = fontSize.textFieldWidth;
			}
		}
		leftSpace = maxSpace;
		//网格宽高转为象素单位
		cellWidth = cellWidth*(Stage.width-leftSpace-36)/(xTo-xFrom);
		cellHeight = cellHeight*(Stage.height-36)/(yTo-yFrom);
		//滑动值转为象素单位
		if (aXLabel.length == 0 || aXLabel.length == 1) {
			xSlide = 0;
		} else {
			xSlide = xSlide*(Stage.width-leftSpace-36)/(aXLabel.length-1)/100;
		}
		if (aYLabel.length == 0 || aYLabel.length == 1) {
			ySlide = 0;
		} else {
			ySlide = ySlide*(Stage.height-36)/(aYLabel.length-1)/100;
		}
		//formatter color
		formatter.color = textColor;
	}
}
